<?
include "libhtml.php";
include "libsession.php";

requireLogin();
echoHeader("Edit Document");

// get the id from the params
if (isset($_GET["stepid"]))
{
	$stepid = $_GET["stepid"];	
}
else
{
	$stepid = 0;	
}

// load the step

if ($stepid > 0)
{
	$sql = "SELECT Id, Name
			FROM WorkflowStep WHERE Id=$stepid;";
	
	$result = query_db($sql);	
	$step = mysql_fetch_array($result, MYSQL_ASSOC);
	mysql_free_result($result);
}

// get a list of users for the actions
$sql = "SELECT Id, Name FROM User";
$users = query_db($sql);
?>

<h1>Edit Workflow Step</h1>
<br><br>
<form action='WorkflowStep_Edit.php?stepid=<? echo $stepid; ?>' method='post'>
	<table>
		<tr>
			<td> Name: </td>
			<td><input type='text' size='100' name="Name" value='<? echo $step["Name"]?>' ></td>
		</tr>
	</table>
</form>
<!-- form action='WorkflowEvent_Edit.php' method=post -->
	<br><br><br>
	<h1>Events</h1>
	<table width=100% border=1>
		<tr>
			<th>Event Type</th>
			<th>Event Date</th>
			<th>Event Update</th>
		</tr>
		<?
			// get the workflow steps associated with this template
			$sql = "SELECT Id, Type, ParamDate FROM WorkflowEvent
			WHERE StepId=$stepid";
			
			$steps = query_db($sql);
			
			while ($event = mysql_fetch_array($steps, MYSQL_ASSOC))
			{
				
				// build the events
				
				$isSelected_StepStart = "";
				$isSelected_Click = "";
				$isSelected_DatePast = "";
				
				if ($event["Type"] == WF_EVENT_TYPE_StepStart)
					$isSelected_StepStart = " selected='selected'";
				else if ($event["Type"] == WF_EVENT_TYPE_Click)
					$isSelected_Click = " selected='selected'";
				else if ($event["Type"] == WF_EVENT_TYPE_DatePast)
					$isSelected_DatePast = " selected='selected'";
				else
					$typetext = "Not Set";
				
				echo "<tr>\n";
				echo "	<form action=WorkflowUpdate.php method=post>\n";
				echo "	<input type=hidden name=action value='event'>\n";
				echo "	<input type=hidden name=targetid value='" . $event["Id"] . "'>\n";
				echo "	<input type=hidden name=sourceurl value='WorkflowStep_Edit.php?stepid=" . $stepid . "'>\n";
				echo "		<td>\n";
				echo "			<select name=Type>\n";
				echo "				<option value=" . WF_EVENT_TYPE_StepStart . $isSelected_StepStart . ">Step Start</option>\n";
				echo "				<option value=" . WF_EVENT_TYPE_Click . $isSelected_Click . ">Click</option>\n";
				echo "				<option value=" . WF_EVENT_TYPE_DatePast . $isSelected_DatePast . ">Date Past</option>\n";
				echo "			</select>\n";
				echo "		</td>\n";
				echo "		<td><input type=text name=ParamDate value='" . $event["ParamDate"] . "'></td>\n";
				echo "		<td><input type=submit value='Update Event'></td>\n";
				echo "	</form>\n";
				echo "</tr>\n";
				
				// build the actions table
				$eventid = $event["Id"];
				$sql = "SELECT Id, Type, ParamStepId, ParamUserId, ParamEmailMsg 
						FROM WorkflowAction WHERE EventId=$eventid";
				$actions = query_db($sql);
				
				// only add to the table if we have actions to show
				if (mysql_num_rows($actions) > 0)
				{
					echo "<tr>\n";
					echo "	<td>&nbsp;</td>";
					echo "	<td colspan=2>\n";
					echo "<h1>Actions</h1>";
					echo "		<table>\n";
					echo "			<tr>\n";
					echo "				<th>Type</th>\n";
					echo "				<th>Step</th>\n";
					echo "				<th>User</th>\n";
					echo "				<th>Email Msg</th>\n";
					echo "				<th>Update</th>\n";
					echo "			</tr>\n";
	
					while ($action = mysql_fetch_array($actions, MYSQL_ASSOC))
					{
						$actiontypetext = "";
						if ($action["Type"] == WF_ACTION_TYPE_ChangeOwner)
							$actiontypetext = "Change Owner";
						else if ($action["Type"] == WF_ACTION_TYPE_SendEmail)
							$actiontypetext = "Send Email";
						else if ($action["Type"] == WF_ACTION_TYPE_Navigate)
							$actiontypetext = "Change Step";
							
						echo "<tr>";
						echo "	<td>" . $actiontypetext . "</td>";
						echo "	<td>" . $action["ParamStepId"] . "</td>";
						echo "	<td>";
						echo "		<select name='Owner'>";
						echo "			<option value=0></option>";
						
						// reset our user list
						mysql_data_seek($users, 0);
						
				      	while ($user=mysql_fetch_array($users, MYSQL_ASSOC))
				      	{
					      	$isSelected = "";
					      	if ($user["Id"] == $action["ParamUserId"])
					      	{
						      	$isSelected = '  selected="selected"';
					      	}
					      	echo "<option value=" . $user["Id"] . $isSelected . ">" . $user["Name"] . "</option>";
				      	}
				      	echo "  	</select>";
				      	echo "	</td>";
						echo "	<td>" . $action["ParamEmailMsg"] . "</td>";
						echo "</tr>";
					}
					
					echo "		</table>";
					echo "	</td>";
					echo "</tr>";
				}
			}
		?>
	</table>
	<div style='text-align: center;'>
		<input type=submit value='Add Event'>
	</div>
<!-- /form -->

<? echoFooter(); ?>